import wx from "weixin-js-sdk";
import { wxJsConfig } from "@/api/config";
import { wxOaCodeUrl, wxOaLogin } from "@/api/login";
import { isIos } from "@/utils/channel";
import { PayStatusEnum } from "@/enums/payEnum";
import { ChannelEnum } from "@/enums/channelEnum";

const wechat = {
    /**
     * 获取公众号授权url
     */
    authCodeUrl(url = '') {
        wxOaCodeUrl({ url: url? url:location.href }).then((res) => {
            location.href = res.url;
        });
    },
    /**
     * 获取初始化页面链接
     * @returns 
     */
    getConfigUrl() {
        return isIos() ? uni.getStorageSync("appIosUrl") : location.href;
    },
    /**
     * 微信初始化
     * @returns 
     */
    config() {
        return new Promise((resolve, reject) => {
            wxJsConfig({
                url: this.getConfigUrl(),
                jsApiList: [
                    "chooseWXPay",
                    "updateAppMessageShareData",
                    "updateTimelineShareData",
                ],
            }).then((res) => {
                wx.config({
                    ...res,
                    success: () => {
                        resolve("success");
                    },
                    fail: (res: any) => {
                        reject("weixin config is fail");
                    },
                });
            });
        });
    },

    /**
     * 公众号授权登录
     */
    authLogin(code) {
        return new Promise((resolve, reject) => {
            wxOaLogin({
                code: code,
                channel: ChannelEnum.WX_OA
            })
                .then((res) => {
                    resolve(res);
                })
                .catch((err) => {
                    reject(err);
                });
        });
    },

    /**
     * 准备就绪
     */
    ready() {
        return new Promise((resolve) => {
            wx.ready(() => {
                resolve("success");
            });
        });
    },


    /**
     * 页面分享
     * @param params 参数
     * @param callback 回调函数
     */
    share(params, callback: any = null) {
        this.ready().then(() => {
            const { shareTitle, shareLink, shareImage, shareDesc } = params;
            // 发送到朋友圈
            wx.updateTimelineShareData({
                title: shareTitle,
                link: shareLink,
                imgUrl: shareImage,
                success: function (): void {
                    callback & callback()
                }
            })
            // 发送给朋友
            wx.updateAppMessageShareData({
                title: shareTitle,
                link: shareLink,
                imgUrl: shareImage,
                desc: shareDesc,
                success: function (): void {
                    callback & callback()
                }
            })
        });
    },

    /**
     * 发起微信支付请求
     * @param params 
     */
    pay(params) {
        return new Promise((resolve, reject) => {
            this.ready()
                .then(() => {
                    wx.chooseWXPay({
                        timestamp: params.timeStamp,
                        nonceStr: params.nonceStr,
                        package: params.package,
                        signType: params.signType,
                        paySign: params.paySign,
                        success: (res: any) => {
                            if (res.errMsg === 'chooseWXPay:ok') {
                                res.callPayStatus = PayStatusEnum.SUCCESS
                                resolve(res)
                            } else {
                                res.callPayStatus = PayStatusEnum.FAIL
                                reject(res)
                            }
                        },
                        cancel: (res: any) => {
                            res.callPayStatus = PayStatusEnum.CANCEL
                            reject(res)
                        },
                        fail: (res: any) => {
                            res.callPayStatus = PayStatusEnum.FAIL
                            reject(res)
                        }
                    })
                })
                .catch((err) => {
                    err.callPayStatus = PayStatusEnum.ERROR
                    reject(err)
                })
        })
    },

    /**
     * 获取地理位置
     */
    getLocation() {
        return new Promise((reslove, reject) => {
            this.ready().then(() => {
                wx.getLocation({
                    type: "gcj02", // wgs84" | "gcj02
                    success: (res) => {
                        reslove(res);
                    },
                    fail: (res) => {
                        reject(res);
                    },
                });
            });
        });
    },
};

export default wechat;
